/**
 * 
 */
package com.ddyx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.ddyx.entity.Customer;
import com.ddyx.entity.Employe;
import com.ddyx.entity.Store;
import com.ddyx.entity.WxUser;
import com.ddyx.mapper.CustomerMapper;
import com.ddyx.mapper.EmployeMapper;
import com.ddyx.mapper.StoreMapper;
import com.ddyx.mapper.WxUserMapper;
import com.ddyx.service.IEmployeService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MQUtil;
import com.ddyx.util.PropertiesUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.util.template.KeyWord;
import com.ddyx.util.template.TemplateMessageParam;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class EmployeServiceImpl implements IEmployeService {
	@Resource
	EmployeMapper employeMapper;
	@Resource
	CustomerMapper customerMapper;
	@Resource
	StoreMapper storeMapper;
	@Resource
	WxUserMapper wxUserMapper;
	@Resource
	JmsTemplate jmsTemplate;
	@Resource
	Destination tmpMsgDestination;
	@Resource
	Destination topicDestination;
	
	
	private static ObjectMapper jsonMapper = new ObjectMapper();
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getGuide(RequestForm param) throws Exception {
		ResponseForm result = new ResponseForm();
		Date date = new Date();
		if(param!=null) {
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			String employNo = (String) paramMap.get("employNo");
			String storeNo = (String) paramMap.get("storeNo");
			String wxUid = (String) paramMap.get("wxUid");
			Map map = new HashMap<>();
//			List list = new ArrayList<>();
//			Map<String, Object> keyword = new HashMap<>();
			try {
				Example e = new Example(Customer.class);
				Criteria criteria = e.createCriteria();
				criteria.andEqualTo("wxUid", wxUid);
				criteria.andEqualTo("storeNo", storeNo);
				List<Customer> customers = customerMapper.selectByExample(e);
				/*if (customers.size()==1||customers.size()>1) {
					result.setCode(EventTag.OlDCUSTOMER);
				}
				为了测试效果暂时不用此处验证
				*/
//				if(customers.size()==0) {
				if(true) {
					result.setCode(EventTag.NEWCUSTOMER);
					map = employeMapper.getGuide(paramMap);
					result.setData(map);
				}
				
				
				//mq同步 客户导购关系
				paramMap.put("actionType", EventTag.CUSTCONTACT);
				paramMap.put("createTime", date);
				MQUtil.sendObMsg(paramMap, jmsTemplate, topicDestination);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			
			
//			//发送 服务通知
//			//获取Store信息
//			TemplateMessageParam tmpMsgParam = new TemplateMessageParam();
//			Store store = new Store();
//			store.setStoreNo(storeNo);
//			store = storeMapper.selectOne(store);
//			WxUser wxUser = new WxUser();
//			wxUser.setWxUid(wxUid);
//			wxUser = wxUserMapper.selectOne(wxUser);
//			if(map!=null&&store!=null) {
//				list.add(new KeyWord((String) map.get("nickName"),EventTag.BLACK));//导购姓名
//				list.add(new KeyWord(store.getStoreName(), EventTag.BLACK));//门店名
//				list.add(new KeyWord(store.getLocation(), EventTag.BLACK)); //门店地址
//				list.add(new KeyWord(store.getPhoneNumber(), EventTag.BLACK));
//				
//				int i = 1;
//				for (Object o : list) {
//					keyword.put("keyword"+i, o);
//					i++;
//				}
//				//设置参数
//				tmpMsgParam.setTouser(wxUser.getOpenId());
//				tmpMsgParam.setForm_id((String) paramMap.get("formId"));
//				tmpMsgParam.setTemplate_id(PropertiesUtil.getProperty(EventTag.GUIDETEMP+"TMP_ID"));
//				tmpMsgParam.setData(keyword);
//				tmpMsgParam.setEmphasis_keyword("keyword2.DATA");
//				String json = jsonMapper.writeValueAsString(tmpMsgParam);
//				//MQ异步发送 
//				MQUtil.sendJsonMsg(json, jmsTemplate, tmpMsgDestination);
//			}else {
//				result.setMessage("操作成功!由于参数错误,服务通知发送失败!!");
//			}
			
			return result;
		}
		
		return null;
	}

}
